package com.lbssoft.gpsbd.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbssoft.gpsbd.model.Log;
import com.lbssoft.gpsbd.model.LoginLog;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.UserVo;
import com.lbssoft.gpsbd.service.LogService;
import com.mysql.jdbc.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("系统日志")
@RestController
@RequestMapping("/logs")
public class LogController {


	@Autowired
	private LogService logService;

	@ApiOperation(value="获取系统日志记录", notes="获取系统日志记录")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "page", value = "当前页",   dataType = "int"),
			@ApiImplicitParam(paramType="query",name = "pageSize", value = "每页显示的条数",  dataType = "int")
	})
	@RequiresPermissions("system:log:view")
	@RequestMapping(value = "",method = {RequestMethod.GET})
	@ResponseBody
	public Result list(@NotEmpty @RequestParam(value = "page", defaultValue = "1") int page,
					   @NotEmpty @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
					   @RequestParam(value = "column", defaultValue = "") String column,
					   @RequestParam(value = "keyword", defaultValue = "") String keyword,
					   @RequestParam(value = "status") Integer status) {

		String where = " where 1=1 ";
		if(!StringUtils.isNullOrEmpty(column)&&!StringUtils.isNullOrEmpty(keyword)){
			where = where+" and "+column+" LIKE '%"+keyword+"%'";
		}
		if(status!=-1){
			where = where+" and status="+status;
		}
		PageHelper.startPage(page, pageSize);
		List<Log> list = logService.list(where);
		PageInfo pageInfo = new PageInfo(list);
		Page pages = (Page) list;
		Result result = new Result();
		Map data = new HashMap();
		data.put("page", page);
		data.put("total", pageInfo.getTotal());
		data.put("list", pages);
		result.setData(data);
		return result;
	}

	@ApiOperation(value="获取登录记录", notes="获取登录记录")
	@RequiresPermissions("system:loginlog:view")
	@RequestMapping(value = "loginlog",method = {RequestMethod.GET})
	@ResponseBody
	public Result loginLogs(@NotEmpty @RequestParam(value = "page", defaultValue = "1") int page,
							@NotEmpty @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
		UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
		PageHelper.startPage(page, pageSize);
		List<LoginLog> list = logService.loginList(user.getId());
		PageInfo pageInfo = new PageInfo(list);
		Page pages = (Page) list;
		Result result = new Result();
		Map data = new HashMap();
		data.put("page", page);
		data.put("total", pageInfo.getTotal());
		data.put("list", pages);
		result.setData(data);
		return result;
	}
	
}